package com.wondersgroup.insurance.dnuilearn.views.shader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.wondersgroup.insurance.dnuilearn.R;

/**
 * Created by Administrator on 2017/5/11.
 */

public class ShaderTest extends View {
    private int[] mColors = {Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW};

    BitmapShader bmpShader = null;
    private Paint mPaint;
    public ShaderTest(Context context) {
        this(context, null);
    }

    public ShaderTest(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShaderTest(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        drawBmpShader(canvas);
//        linearGradient(canvas);
//        radialGradient(canvas);
//        sweepGradient(canvas);
        composeShader(canvas);
    }

    void drawBmpShader(Canvas canvas){
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.xyjy2);
        bmpShader = new BitmapShader(bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP );
//        bmpShader = new BitmapShader(bmp, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR );

        Matrix matrix = new Matrix();
        matrix.setScale(2,2);
//        bmpShader.setLocalMatrix(matrix);
        mPaint.setShader(bmpShader);
        Rect rect = new Rect(0,0,bmp.getWidth()*2,bmp.getHeight()*2);
        canvas.drawRect(rect, mPaint);
        int radius = Math.max(bmp.getWidth(),bmp.getHeight() )/2;
//        canvas.drawCircle(radius,radius, radius,mPaint);
//        RectF rectF = new RectF(rect);
//        canvas.drawOval(rectF,mPaint);//画椭圆 一样宽高就是圆


        ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
        shapeDrawable.getPaint().setShader(bmpShader);
        shapeDrawable.setBounds(rect);

        shapeDrawable.draw(canvas);
    }

    void linearGradient(Canvas canvas){
        /**线性渐变
         * x0, y0, 起始点
         *  x1, y1, 结束点
         * int[]  mColors, 中间依次要出现的几个颜色
         * float[] positions,数组大小跟colors数组一样大，中间依次摆放的几个颜色分别放置在那个位置上(参考比例从左往右)
         *    tile
         */
		LinearGradient linearGradient = new LinearGradient( 0, 0,800, 800, mColors, null, Shader.TileMode.CLAMP);
        // linearGradient = new LinearGradient(0, 0, 400, 400, mColors, null, Shader.TileMode.REPEAT);
		mPaint.setShader(linearGradient);
		canvas.drawRect(0, 0, 800, 800, mPaint);
    }

    void radialGradient(Canvas canvas){
        RadialGradient mRadialGradient = new RadialGradient(300, 300, 50, mColors, null, Shader.TileMode.REPEAT);
        mPaint.setShader(mRadialGradient);
        canvas.drawCircle(300, 300, 300, mPaint);
    }

    void sweepGradient(Canvas canvas){

        SweepGradient mSweepGradient = new SweepGradient(300, 300, mColors, null);
		mPaint.setShader(mSweepGradient);
		canvas.drawCircle(300, 300, 300, mPaint);
//        canvas.drawRect(0, 0, 800, 800, mPaint);
    }

    void composeShader(Canvas canvas){
        LinearGradient linearGradient = new LinearGradient( 0, 0,800, 800, mColors, null, Shader.TileMode.CLAMP);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.xyjy2);
        bmpShader = new BitmapShader(bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP );
        ComposeShader mComposeShader = new ComposeShader(linearGradient, bmpShader, PorterDuff.Mode.MULTIPLY);
        mPaint.setShader(mComposeShader);
        canvas.drawRect(0, 0, 800, 1000, mPaint);
    }
}
